package com.netty.demo;

import java.nio.ByteBuffer;

/**
 * @Auther: Admin
 * @Date: 2021/4/14 10:28
 * @Description:
 */
public class TestByteBufferPackage {


    public static void main(String[] args) {

        ByteBuffer buffer = ByteBuffer.allocate(64);
        buffer.put("nihao\nwoshi\nndf".getBytes());
        split(buffer);
        buffer.put("123132\n".getBytes());
        split(buffer);

    }


    public static void split(ByteBuffer byteBuffer){

        byteBuffer.flip();// 切换为读模式

        for (int i = 0; i < byteBuffer.limit(); i++) {


            if(byteBuffer.get(i) == '\n'){
                // 获取读取长度
                int len = i + 1 - byteBuffer.position();

                for (int j = 0; j < len; j++) {
                    System.out.println((char) byteBuffer.get());
                }

                System.out.println("==============");

            }

        }

        byteBuffer.compact();// 压缩，取未读数据

    }

}
